//
//  CustomQualityView.m
//  YouinRTCDemo
//
//  Created by 潘欣 on 2023/11/13.
//

#import "CustomQualityView.h"
#import <LSTPopView.h>
@interface CustomQualityView ()
//成员变量用weak修饰, 不可以用strong修饰
@property (nonatomic,weak) LSTPopView *popView;
@property (nonatomic,strong)UITextField *videoWidth;
@property (nonatomic,strong)UITextField *videoHeight;
@property (nonatomic,strong)UITextField *videokbps;//码率
@end

@implementation CustomQualityView


- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
       
        self.backgroundColor = [UIColor whiteColor];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"画面质量设置";
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor blackColor];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self).offset(14);
        }];
        
        
        UILabel *label2 = [[UILabel alloc] init];
        label2.text = @"视频宽度：";
        label2.font = [UIFont systemFontOfSize:15];
        label2.textColor = [UIColor blackColor];
        [label2 setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

        [self addSubview:label2];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(17);
            make.top.mas_equalTo(label.mas_bottom).offset(25);
        }];
        
        self.videoWidth = [[UITextField alloc] init];
        self.videoWidth.placeholder = @"视频宽度不超过1920";
        if (@available(iOS 13.0, *)) {
            self.videoWidth.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
        } else {
            // Fallback on earlier versions
        }
        self.videoWidth.font = [UIFont systemFontOfSize:14];
//        self.videoWidth.backgroundColor = [UIColor colorWithRed:246/255.0 green:247/255.0 blue:251/255.0 alpha:1.0];
        self.videoWidth.leftViewMode=UITextFieldViewModeAlways;
        self.videoWidth.layer.cornerRadius = 5;
        self.videoWidth.keyboardType = UIKeyboardTypeNumberPad;
        self.videoWidth.leftView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        [self addSubview:self.videoWidth];
        [self.videoWidth mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label2.mas_right).offset(5);
            make.right.mas_equalTo(self.mas_right).offset(-20);
            make.centerY.mas_equalTo(label2);
            make.height.offset(40);
        }];
        
        
        //视频高度
        UILabel *label_height = [[UILabel alloc] init];
        label_height.text = @"视频高度：";
        label_height.font = [UIFont systemFontOfSize:15];
        label_height.textColor = [UIColor blackColor];
        [label_height setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

        [self addSubview:label_height];
        [label_height mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(17);
            make.top.mas_equalTo(label2.mas_bottom).offset(35);
        }];
        
        self.videoHeight = [[UITextField alloc] init];
        self.videoHeight.placeholder = @"视频高度不超过1080";
        if (@available(iOS 13.0, *)) {
            self.videoHeight.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
        } else {
            // Fallback on earlier versions
        }
        self.videoHeight.font = [UIFont systemFontOfSize:14];
//        self.videoHeight.backgroundColor = [UIColor colorWithRed:246/255.0 green:247/255.0 blue:251/255.0 alpha:1.0];
        self.videoHeight.leftViewMode=UITextFieldViewModeAlways;
        self.videoHeight.layer.cornerRadius = 5;
        self.videoHeight.leftView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        self.videoHeight.keyboardType = UIKeyboardTypeNumberPad;
        [self addSubview:self.videoHeight];
        [self.videoHeight mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label_height.mas_right).offset(5);
            make.right.mas_equalTo(self.mas_right).offset(-20);
            make.centerY.mas_equalTo(label_height);
            make.height.offset(40);
        }];
        
        
        //视频高度
        UILabel *label_kbps = [[UILabel alloc] init];
        label_kbps.text = @"编码码率(kbps)：";
        label_kbps.font = [UIFont systemFontOfSize:15];
        label_kbps.textColor = [UIColor blackColor];
        [label_kbps setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

        [self addSubview:label_kbps];
        [label_kbps mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(17);
            make.top.mas_equalTo(label_height.mas_bottom).offset(35);
        }];
        
        self.videokbps = [[UITextField alloc] init];
        self.videokbps.placeholder = @"编码码率不超过5000";
        self.videokbps.font = [UIFont systemFontOfSize:14];
        if (@available(iOS 13.0, *)) {
            self.videokbps.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
        } else {
            // Fallback on earlier versions
        }
//        self.videoHeight.backgroundColor = [UIColor colorWithRed:246/255.0 green:247/255.0 blue:251/255.0 alpha:1.0];
        self.videokbps.leftViewMode=UITextFieldViewModeAlways;
        self.videokbps.layer.cornerRadius = 5;
        self.videokbps.leftView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        self.videokbps.keyboardType = UIKeyboardTypeNumberPad;
        [self addSubview:self.videokbps];
        [self.videokbps mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(label_kbps.mas_right).offset(5);
            make.right.mas_equalTo(self.mas_right).offset(-20);
            make.centerY.mas_equalTo(label_kbps);
            make.height.offset(40);
        }];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = rgba(0, 145, 255, 1);
        [btn setTitle:@"确定" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.layer.cornerRadius = 24;
        [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
        btn.clipsToBounds = YES;
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.offset(240);
            make.height.offset(48);
            make.left.mas_equalTo(self.mas_left).offset(68);
            make.right.mas_equalTo(self.mas_right).offset(-68);
            make.bottom.mas_equalTo(self.mas_bottom).offset(-44);
        }];
        
    }
    return self;
}
- (void)btnAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(qualityDelegateWithWidth:withHeight:withKbps:)]) {
        [self.delegate qualityDelegateWithWidth:[self.videoWidth.text intValue] withHeight:[self.videoHeight.text intValue] withKbps:[self.videokbps.text intValue]];
    }
    [self.popView dismiss];
}


-(void)show
{
    LSTPopView *popView = [LSTPopView initWithCustomView:self
                                                popStyle:LSTPopStyleSmoothFromBottom
                                            dismissStyle:LSTDismissStyleSmoothToBottom];

    popView.hemStyle = LSTHemStyleBottom;
    popView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    LSTPopViewWK(popView)
    popView.popDuration = 0.5;
    popView.dismissDuration = 0.5;
    popView.isClickFeedback = YES;
    popView.avoidKeyboardSpace = 0;
    popView.rectCorners = UIRectCornerTopLeft | UIRectCornerTopRight;
    popView.cornerRadius = 15;
    popView.sweepStyle = LSTSweepStyleY_Positive;
//    popView.dragStyle = LSTDragStyleY_Positive;
//    popView.sweepDismissStyle = LSTSweepDismissStyleSmooth;
    popView.bgClickBlock = ^{
        NSLog(@"点击了背景");
        if (self.delegate && [self.delegate respondsToSelector:@selector(qualityDelegateWithWidth:withHeight:withKbps:)]) {
            [self.delegate qualityDelegateWithWidth:[self.videoWidth.text intValue] withHeight:[self.videoHeight.text intValue] withKbps:[self.videokbps.text intValue]];
        }
        [wk_popView dismiss];
    };
    self.popView = popView;

    [popView pop];
}
-(void)dealloc
{
    NSLog(@"dealloc");
}

@end
